package de.tabuma.wsdl2xform;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.util.logging.*;

import com.google.appengine.api.users.*;

import de.tabuma.formcreator.entity.*;
import de.tabuma.formcreator.logik.*;
import de.tabuma.soap.*;
import de.tabuma.soap.entity.wsdl.*;
import de.tabuma.soap.logik.*;
import de.tabuma.wsdl2xform.xform.entity.Project;
import de.tabuma.wsdl2xform.xform.logik.ProjectFacade;
import de.tabuma.shopping.logik.LicenseUseFacade;

/**
 *
 * @author alefen
 */
public class wsdl2xform extends HttpServlet {

    private final static Logger log = Logger.getLogger(wsdl2xform.class.getName());

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            log.fine("Getting request");

            int projectID = 0;
            if(request.getParameter(de.tabuma.wsdl2xform.xform.constants.PROJECT_ID)!=null) {
                projectID = Integer.valueOf(request.getParameter(de.tabuma.wsdl2xform.xform.constants.PROJECT_ID));
            }

            log.fine("projectID = "+projectID);


            if(projectID!=0) {

                log.fine("loading project");

                Project project = ProjectFacade.getProject(projectID);
                if(project!=null) {

                    log.fine("loading wsdl file from project");
                    //1. Schritt laden der Url
                    TDefinitions wsdl = de.tabuma.soap.logik.PMF.get(project.getWsdlFile(), new TDefinitions());

                    log.fine("loading formular manager");
                    //2. Schritt Initialisierung des FormularManagers
                    FormManager formularManager = FormManagerFactory.createFormularManager(wsdl, project.getMethod());

                    log.fine("getting css links");
                    //3. Ausgabe des XForm Dokuments
                    String cssLink = (String) request.getParameter("css");
                    if(cssLink==null) {
                        cssLink = "style.css";
                    }

                    log.fine("printing formular content");

                    //LicenseUseFacade.create(project.getId().intValue(), "Open " + project.getName(), de.tabuma.pm.constants.PM_SHOPPING_VIEW_SOAP_LICENSE, de.tabuma.pm.constants.PM_SHOPPING_VIEW_SOAP_LICENSE, request.getRemoteHost()+"/"+request.getRemoteHost());
                    out.println(FormManagerFacade.getXForm(request, formularManager,"dataservlet","parent.iframeName" , cssLink, project.getWsdlFile() , project.getMethod()));

                    
                }
                else {
                    out.println("No project found for parameter <i>" + de.tabuma.wsdl2xform.xform.constants.PROJECT_ID + "="+projectID+"</i> .");
                }
            }
            else {
                out.println("No parameter <i>" + de.tabuma.wsdl2xform.xform.constants.PROJECT_ID + "</i> found.");
            }
        }
        catch(Exception e) {
            out.println("Error.");
            log.warning(e.getMessage()+"/"+e.getCause()+"/"+e.getClass());
            String warning = "";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);

        }
        finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}